﻿namespace HutongGames.PlayMakerEditor
{
    using HutongGames.PlayMaker;
    using System;
    using UnityEngine;

    internal static class FsmDrawState
    {
        public static DrawState GetDrawState(Fsm fsm)
        {
            if ((fsm != null) && fsm.Active)
            {
                return GetDrawState(fsm, false, fsm.Active, Fsm.BreakAtFsm == fsm, false);
            }
            return DrawState.Normal;
        }

        public static DrawState GetDrawState(Fsm fsm, FsmState state, FsmStateAction action)
        {
            if (fsm != null)
            {
                if (!Application.isPlaying)
                {
                    return DrawState.Normal;
                }
                if (!state.Active || !action.Active)
                {
                    return DrawState.Normal;
                }
                if (GameStateTracker.CurrentState != GameState.Break)
                {
                    if (GameStateTracker.CurrentState == GameState.Paused)
                    {
                        return DrawState.Paused;
                    }
                    if (GameStateTracker.CurrentState == GameState.Running)
                    {
                        return DrawState.Active;
                    }
                    if (GameStateTracker.CurrentState == GameState.Error)
                    {
                        return DrawState.Error;
                    }
                }
            }
            return DrawState.Normal;
        }

        private static DrawState GetDrawState(Fsm fsm, bool selected, bool active, bool isBreakpoint, bool invalid)
        {
            if (invalid)
            {
                return DrawState.Error;
            }
            switch (GameStateTracker.CurrentState)
            {
                case GameState.Running:
                    if (!active)
                    {
                        break;
                    }
                    return DrawState.Active;

                case GameState.Break:
                    if ((Fsm.BreakAtFsm != fsm) || !isBreakpoint)
                    {
                        if (!active)
                        {
                            break;
                        }
                        return DrawState.Paused;
                    }
                    return DrawState.Breakpoint;

                case GameState.Paused:
                    if (!active)
                    {
                        break;
                    }
                    return DrawState.Paused;

                case GameState.Error:
                    if ((Fsm.BreakAtFsm != fsm) || !isBreakpoint)
                    {
                        if (active)
                        {
                            return DrawState.Paused;
                        }
                        break;
                    }
                    return DrawState.Error;
            }
            if (!selected)
            {
                return DrawState.Normal;
            }
            return DrawState.Selected;
        }

        public static DrawState GetFsmStateDrawState(Fsm fsm, FsmState state, bool selected)
        {
            bool active = (fsm.ActiveState == state) && fsm.Active;
            bool isBreakpoint = Fsm.BreakAtState == state;
            return GetDrawState(fsm, selected, active, isBreakpoint, false);
        }

        public static DrawState GetFsmTransitionDrawState(Fsm fsm, FsmTransition transition, bool selected)
        {
            bool active = false;
            if (fsm.SwitchedState || fsm.HitBreakpoint)
            {
                active = (fsm.LastTransition == transition) && fsm.Active;
            }
            return GetDrawState(fsm, selected, active, false, false);
        }
    }
}

